package com.sbl.bajw.pc.action;

import java.io.OutputStream;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sbl.bajw.common.ExcelUtil;
import com.sbl.bajw.common.web.JsonResultUtil;
import com.sbl.bajw.common.web.RequestUnit;
import com.sbl.bajw.pc.service.StatisticsService;
import com.sbl.bajw.scheduler.StatisticsJob;

@Controller
@RequestMapping("/Statistics")
public class StatisticsAction {
	
	@Resource
	private StatisticsService service;

	@Resource
	private StatisticsJob StatisticsService;
	
	@RequestMapping(value = "/StatisticsCameraAndHouse")
	public @ResponseBody String StatisticsCameraAndHouse(HttpServletRequest request, HttpServletResponse response) throws Exception{
		HashMap<String, Object> parm = RequestUnit.getHashMapParmO();
		String result = service.StatisticsCameraAndHouse(parm);
		return result;
	}
	
	/**
	 * @标题：getPcsCameraAndHouseUserList
	 * @作者：罗琦
	 * @时间：2015年6月2日
	 * @作用：摄像头和监控室采集，派出所所有人员采集统计列表
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getPcsCameraAndHouseUserList")
	public @ResponseBody String getPcsCameraAndHouseUserList(HttpServletRequest request, HttpServletResponse response) throws Exception{
		HashMap<String, Object> parm = RequestUnit.getHashMapParmO();
		String result = service.getPcsCameraAndHouseUserList(parm);
		return result;
	}
	
	@RequestMapping(value = "/StatisticsJQ")
	public @ResponseBody String StatisticsJQ(HttpServletRequest request, HttpServletResponse response) throws Exception{
		HashMap<String, Object> parm = RequestUnit.getHashMapParmO();
		String result = service.StatisticsJQ(parm);
		return result;
	}
	
	/**
	 * @标题：StatisticsCar
	 * @作者：罗琦
	 * @时间：2015年5月19日
	 * @作用：静态车辆采集每日统计列表
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/StatisticsCar")
	public @ResponseBody String StatisticsCar(HttpServletRequest request, HttpServletResponse response){
		HashMap<String, Object> parm = RequestUnit.getHashMapParmO();
		String json = service.StatisticsCar(parm);
		return json;
	}

	/**
	 * @标题：getPcsCarUserStatisticsList
	 * @作者：罗琦
	 * @时间：2015年6月2日
	 * @作用：静态车辆采集，派出所所有用户采集统计列表
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getPcsCarUserStatisticsList")
	public @ResponseBody String getPcsCarUserStatisticsList(HttpServletRequest request, HttpServletResponse response){
		HashMap<String, Object> parm = RequestUnit.getHashMapParmO();
		String json = service.getPcsCarUserStatisticsList(parm);
		return json;
	}
	
	/**
	 * @标题：StatisticsCarJq
	 * @作者：罗琦
	 * @时间：2015年5月26日
	 * @作用：社区级静态车辆采集每日统计列表
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/StatisticsCarJq")
	public @ResponseBody String StatisticsCarJq(HttpServletRequest request, HttpServletResponse response){
		HashMap<String, Object> parm = RequestUnit.getHashMapParmO();
		String json = service.StatisticsCarJq(parm);
		return json;
	}
	
	/**
	 * @标题：StatisticsUser
	 * @作者：罗琦
	 * @时间：2015年5月27日
	 * @作用：用户安装APP统计数据列表展示
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/StatisticsUser")
	public @ResponseBody String StatisticsUser(HttpServletRequest request, HttpServletResponse response){
		HashMap<String, Object> parm = RequestUnit.getHashMapParmO();
		String json = service.StatisticsUser(parm);
		return json;
	}
	
	@RequestMapping(value = "/getCameraUserStatisticsList")
	public @ResponseBody String getCameraUserStatisticsList(HttpServletRequest request, HttpServletResponse response) throws Exception{
		HashMap<String, Object> parm = RequestUnit.getHashMapParmO();
		String result = service.getCameraUserStatisticsList(parm);
		return result;
	}
	
	/**
	 * @标题：getCarUserStatisticsList
	 * @作者：罗琦
	 * @时间：2015年5月20日
	 * @作用：社区、综管所人员采集车辆统计列表
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getJqCarUserStatisticsList")
	public @ResponseBody String getJqCarUserStatisticsList(HttpServletRequest request, HttpServletResponse response){
		HashMap<String, Object> parm = RequestUnit.getHashMapParmO();
		String result = service.getJqCarUserStatisticsList(parm);
		return result;
	}
	
	/**
	 * @标题：publicView
	 * @作者：罗琦
	 * @时间：2015年5月27日
	 * @作用：APP统计数据列表公共展示
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/publicView")
	public ModelAndView publicView(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("/view/manager/public/statistics");
		return mv;
	}
	
	/**
	 * @标题：publicUserView
	 * @作者：罗琦
	 * @时间：2015年5月27日
	 * @作用：用户安装APP数量统计列表APP展示
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/publicUserView")
	public ModelAndView publicUserView(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("/view/manager/public/userStatistics");
		return mv;
	}
	
	@RequestMapping(value = "/ReCameraHouseStatistics")
	public @ResponseBody String ReCameraHouseStatistics(HttpServletRequest request, HttpServletResponse response) throws Exception{
		StatisticsService.ReCameraHouseStatistics();
		return JsonResultUtil.Success();
	}
	
	/**
	 * 监控室，摄像头每日统计导出
	 * @作者：罗琦
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/downloadCameraAndHouseStatistics")
	public void downloadCameraAndHouseStatistics(HttpServletRequest request, HttpServletResponse response) throws Exception{
		HashMap<String, Object> parm = RequestUnit.getHashMapParmO();
		List<HashMap<String, Object>> list = service.downloadCameraAndHouseStatistics(parm);
		LinkedHashMap<String, String> column = new LinkedHashMap<String, String>();
		column.put("DEPTNAME", "单位");
		column.put("USER_COUNT", "安装APP数");
		column.put("CAMERA_COUNT", "摄像头总数");
		column.put("CAMERA_CHECK_COUNT", "审核数量");
		column.put("CAMERA_PASS_COUNT", "通过数量");
		column.put("CAMERA_UNPASS_COUNT", "未通过数量");
		column.put("HOUSE_COUNT", "监控室总数");
		column.put("HOUSE_CHECK_COUNT", "审核数量");
		column.put("HOUSE_PASS_COUNT", "通过数量");
		column.put("HOUSE_UNPASS_COUNT", "未通过数量");
		HSSFWorkbook wb = ExcelUtil.createExcel(list, column);
		OutputStream outStream = response.getOutputStream();
		response.setContentType("application/x-msdownload");  
		response.setHeader("Content-Disposition","attachment;filename=CameraAndHouseStatistics.xls");  
		wb.write(outStream);
	}
	
	@RequestMapping(value = "/downloadStatisticsJQ")
	public void downloadStatisticsJQ(HttpServletRequest request, HttpServletResponse response) throws Exception{
		HashMap<String, Object> parm = RequestUnit.getHashMapParmO();
		List<HashMap<String, Object>> list = service.downloadStatisticsJQ(parm);
		LinkedHashMap<String, String> column = new LinkedHashMap<String, String>();
		column.put("DEPTNAME", "单位");
		column.put("PCS", "上级单位");
		column.put("USER_COUNT", "安装APP数");
		column.put("CAMERA_COUNT", "摄像头总数");
		column.put("CAMERA_CHECK_COUNT", "审核数量");
		column.put("CAMERA_PASS_COUNT", "通过数量");
		column.put("CAMERA_UNPASS_COUNT", "未通过数量");
		column.put("HOUSE_COUNT", "监控室总数");
		column.put("HOUSE_CHECK_COUNT", "审核数量");
		column.put("HOUSE_PASS_COUNT", "通过数量");
		column.put("HOUSE_UNPASS_COUNT", "未通过数量");
		HSSFWorkbook wb = ExcelUtil.createExcel(list, column);
		OutputStream outStream = response.getOutputStream();
		response.setContentType("application/x-msdownload");  
		response.setHeader("Content-Disposition","attachment;filename=StatisticsJQ.xls");  
		wb.write(outStream);
	}
	
	/**
	 * @作者：罗琦
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/downloadCameraUserStatistics")
	public void downloadCameraUserStatistics(HttpServletRequest request, HttpServletResponse response) throws Exception{
		HashMap<String, Object> parm = RequestUnit.getHashMapParmO();
		List<HashMap<String, Object>> list = service.downloadCameraUserStatistics(parm);
		LinkedHashMap<String, String> column = new LinkedHashMap<String, String>();
		column.put("SHOWNAME", "姓名");
		column.put("DEPTNAME", "单位");
		column.put("CAMERA_COUNT", "摄像头总数");
		column.put("CAMERA_PASSCOUNT", "摄像头合格数量");
		column.put("CAMERA_UNPASS_COUNT", "未通过数量");
		column.put("HOUSE_COUNT", "监控室总数");
		column.put("HOUSE_PASSCOUNT", "监控室合格数量");
		HSSFWorkbook wb = ExcelUtil.createExcel(list, column);
		OutputStream outStream = response.getOutputStream();
		response.setContentType("application/x-msdownload");  
		response.setHeader("Content-Disposition","attachment;filename=CameraUserStatistics.xls");  
		wb.write(outStream);
	}
}
